﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NET.Tools.OS;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace ResolutionManager
{
    public enum ResolutionType
    {
        Resolution3D,
        Resolution2D
    }

    public static class IO
    {
        private const String FILENAME = "config.dat";
        private static String DIRECTORY
        {
            get
            {
                return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\PC-Software\\Resolution-Manager";
            }
        }

        public static void Save(ICollection list)
        {
            using (FileStream fs = CreateConfigurationStream(FileMode.Create))
            {
                using (BinaryWriter writer = new BinaryWriter(fs))
                {
                    foreach (Object item in list)
                    {
                        ResolutionData data = new ResolutionData((DisplayScreen)item);
                        data.Save(writer);
                    }
                }
            }
        }

        public static List<ResolutionData> Load()
        {
            List<ResolutionData> result = new List<ResolutionData>();

            try
            {
                using (FileStream fs = CreateConfigurationStream(FileMode.Open))
                {
                    using (BinaryReader reader = new BinaryReader(fs))
                    {
                        while (fs.Position < fs.Length)
                        {
                            result.Add(new ResolutionData(reader));
                        }
                    }
                }
            }
            catch (IOException e)
            {
                MessageBox.Show("Cannot load settings. Use default and add empty settings file!\n\n(" + e.Message + ")", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                using (File.Create(DIRECTORY + "\\" + FILENAME))
                {
                    //Empty
                }
            }

            return result;
        }

        /// <summary>
        /// Gets the list of given resultion type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Dictionary<ResolutionType, List<DisplayScreen>> GetResolutionList()
        {
            List<ResolutionData> resolutions3D = Load();
            List<DisplayScreen> result2D = new List<DisplayScreen>();
            List<DisplayScreen> result3D = new List<DisplayScreen>();

            foreach (DisplayScreen resolution in NET.Tools.OS.ResolutionManager.ResolutionList)
            {
                bool found = false;

                foreach (ResolutionData data in resolutions3D)
                {
                    if (data.Equals(resolution))
                    {
                        result3D.Add(resolution);
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    result2D.Add(resolution);
                }
            }

            Dictionary<ResolutionType, List<DisplayScreen>> result = new Dictionary<ResolutionType, List<DisplayScreen>>();
            result.Add(ResolutionType.Resolution2D, result2D);
            result.Add(ResolutionType.Resolution3D, result3D);

            return result;
        }

        private static FileStream CreateConfigurationStream(FileMode mode)
        {
            if (!Directory.Exists(DIRECTORY))
            {
                Directory.CreateDirectory(DIRECTORY);
            }

            return new FileStream(DIRECTORY + "\\" + FILENAME, mode);
        }
    }
}
